<?php
/*******************************************************************************
 * $Date:: Sep 29, 2011 10:49:20 AM                                              $
 * $Revision:                                                                  $
 * $Author:: Tuan Duong                                                        $
 * $Comment::                                                                  $
 ******************************************************************************/ 
/**
 * Ajax controller: Handle ajax requests
 * 
 * @package Lua
 * @author Tuan Duong <bacduong[at]gmail[dot]com>
 * @version 
 */

class AjaxController extends Lua_Controller_Default
{
    public function init(){
        if (!Lua_Request::getInstance()->isXmlHttpRequest()) {
            throw new Lua_Exception("This page can't access directly");
        }
        parent::init();
        $this->view->disableLayout();
    }
    
    /**
     * Login Action
     */
    public function loginAction()
    {
        $this->view->setNoRender();
        $username = Lua_Request::getInstance()->getPost('username', '');
        $password = Lua_Request::getInstance()->getPost('password', '');
        if ('' == $username || '' == $password) {
            throw new Lua_Exception("Please input username or password");
        }
        
        $auth = new Lua_Auth_User($username, $password);
        $result = Zend_Auth::getInstance()->authenticate($auth);
        if ($result->isValid()) {
            echo "Ok";
        } else {
            echo "Wrong username or password";
        }
    }
    
    public function profileBoxAction()
    {
        if (!$this->isLogged()) {
            throw new Lua_Exception("Please login first.");
        }   
        $this->view->setNoRender();
        echo $this->view->render("./partials/profile-box.phtml");
    }
    
    public function logoutAction()
    {
        Zend_Auth::getInstance()->clearIdentity();
        echo "Ok";
    }
}